/*******************************************************************************
 * Copyright (c) 2000, 2003 FUJITSU LIMITED and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     FUJITSU LIMITED - initial implementation
 *******************************************************************************/

package org.eclipse.cobol.ui.common.text;

import org.eclipse.cobol.ui.common.IReferenceFormatConstants;

/**
 * An implementation of <code>IWordDetector</code> by which WordRule determines
 * whether a given character is a valid character as a part of a word in the
 * current context.
 * 
 * @version 1.1
 * @author Fujitsu
 */
public class COBOLWordDetector implements ICOBOLWordDetector
{

	/**
	 * The reference format
	 */
	private String fRefFormat;

	/**
	 * Creates an instance of COBOL Word Detector
	 * 
	 * @param column the column number up to which the word rule applies
	 */
	public COBOLWordDetector(String format)
	{
		fRefFormat = format;
	}

	/**
	 * Returns whether the starting character of a word is a letter or not
	 * 
	 * @param character the character to be checked
	 * @return true if the character is valid as a start of the word
	 */
	@Override
	public boolean isWordStart(char character)
	{

		return (character == '_' || character == '-' || Character.isLetter(character) || Character.isDigit(character));
	}

	/**
	 * Returns whether the starting character of a word is a letter or not till
	 * the specified column number.
	 * 
	 * @param character the character to be checked
	 * @param column checks till this column
	 * @return true if the character is valid as a start of the word
	 */
	@Override
	public boolean isWordStart(char character, int column)
	{

		// Column Number is mainly used for Program Identification Area coloring
		int limit = 0;
		if (fRefFormat.equalsIgnoreCase(IReferenceFormatConstants.FIXED_FORMAT))
		{
			limit = ICOBOLWordDetector.FIXED_PID_AREA_BEGIN;
		}
		else if (fRefFormat.equalsIgnoreCase(IReferenceFormatConstants.VARIABLE_FORMAT))
		{
			limit = ICOBOLWordDetector.VARIABLE_PID_AREA;
		}
		if (column < limit)
		{
			return (character == '_' || character == '-' || Character.isLetter(character) || Character
					.isDigit(character));
		}
		else
		{
			return false;
		}

	}

	/**
	 * Returns whether the specified character is valid as a subsequent
	 * character in a word
	 * 
	 * @param character the character to be checked
	 * @return true if the character is valid as a part of the word
	 */
	@Override
	public boolean isWordPart(char character)
	{

		if (character == '_' || character == '-' || Character.isLetter(character) || Character.isDigit(character))
		{
			return true;
		}
		else
		{
			return false;
		}

	}

	/**
	 * Returns whether the specified character is valid as a subsequent
	 * character in a word till the specified column
	 * 
	 * @param character the character to be checked
	 * @param column checks till this column
	 * @return true if the character is valid as a part of the word
	 */
	@Override
	public boolean isWordPart(char character, int column)
	{

		// Column Number is mainly used for Program Identification Area coloring

		int limit = 0;
		if (fRefFormat.equalsIgnoreCase(IReferenceFormatConstants.FIXED_FORMAT))
		{
			limit = ICOBOLWordDetector.FIXED_PID_AREA_BEGIN;
		}
		else if (fRefFormat.equalsIgnoreCase(IReferenceFormatConstants.VARIABLE_FORMAT))
		{
			limit = ICOBOLWordDetector.VARIABLE_PID_AREA;
		}

		if ((column < limit)
				&& (character == '_' || character == '-' || Character.isLetter(character) || Character
						.isDigit(character)))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

}
